class CarouselsController < ApplicationController
  wechat_api
  before_action :set_shopping_number, only: [:group_index,:promotion_index]

  #分组列表
  def group_index
    @title = '商品列表'

    req_params = {:product_group_id => params[:product_group_id],:sort_type=> 0}
    @product_list = ElephantServiceHelper.get_data('v1,HomePage,product_group_product_list', req_params)
    if @product_list.class != Array
      @product_list = []
    end
  end


  #活动列表
  def promotion_index
    @title = '商品列表'
    @userinfo = current_userinfo

    @off_promotion = OffPromotion.find(params[:off_promotion_id])
    product_group_ids = OffPromotionSubject.where({:off_promotion_id => @off_promotion.id}).collect { |u| u['product_group_id'] }
    product_ids = OffPromotionSubjectProduct.where({:product_group_id => {'$in' => product_group_ids}}).collect { |u| BSON::ObjectId(u.product_id) }
    @product_list = Product.shop_id(@userinfo.id.to_s).where({:_id => {'$in' => product_ids},:state_id => State.online.id}).to_a

    Rails.logger.info "商品分组列表为:#{@product_list.to_json}"

  end


  private
  # Use callbacks to share common setup or constraints between actions.
  def set_shopping_number

    wechat_session = current_wechat_session
    userinfo = current_userinfo
    customer_id = wechat_session.customer_id.present? ? wechat_session.customer_id : wechat_session.business_id

    #获取购物车数量
    req_params = {:customer_id => customer_id,:userinfo_id=> userinfo.id.to_s}
    result_data = ElephantServiceHelper.get_data('v2,shopping_cart,number', req_params)
    @shoppingcart_number = result_data['number']

  end

end